/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine - graphics/context
// Copyright (c) 2015.  All Rights Reserved
//
// File:		AEContextViewProperties.cpp
// Author:		Gianluca Belardelli
// Date:		12/12/2014
//
/////////////////////////////////////////////////////////////////////////
#include <AEEngine.h>

AEContextViewProperties::AEContextViewProperties( void )
{
	m_matCachedMatrix[0].SetIdentity();
	m_matCachedMatrix[1].SetIdentity();

	m_eProjectionType = AEPROJECTIONTYPE_PERSPECTIVE;
	m_bIsDirty = AEFALSE;
	
	m_fNear = 0.0f;
	m_fFar = 0.0f;
	
	m_fFovX = 0.0f;
	m_fFovY = 0.0f;
	m_fDisplayAspectRatio = 0.0f;
	m_fLODScale = 0.0f;
	
	m_fOrthographicWidth = 0.0f;
	m_fOrthographicHeight = 0.0f;
}

const AEMatrix4f &AEContextViewProperties::GetProjectionMatrix( AEClipSpaceYRange::Enum yRange )
{
	if( m_eProjectionType != AEPROJECTIONTYPE_CUSTOM )
	{
		if( m_bIsDirty )
		{
			if( m_eProjectionType == AEPROJECTIONTYPE_PERSPECTIVE || m_eProjectionType == AEPROJECTIONTYPE_PERSPECTIVE_AUTOFRUSTUM )
			{
				m_matCachedMatrix[0].SetPerspectiveProjectionMatrixFromFovY( m_fFovY, m_fDisplayAspectRatio, m_fNear, m_fFar );
				m_matCachedMatrix[1].SetPerspectiveProjectionMatrixFromFovY( m_fFovY, m_fDisplayAspectRatio, m_fNear, m_fFar, AEClipSpaceDepthRange::Native, AEClipSpaceYRange::OneToMinusOne );
			}
			else if( m_eProjectionType == AEPROJECTIONTYPE_ORTHOGRAPHIC )
			{
				m_matCachedMatrix[0].SetOrthographicProjectionMatrix( m_fOrthographicWidth, m_fOrthographicHeight, m_fNear, m_fFar );
				m_matCachedMatrix[1].SetOrthographicProjectionMatrix( m_fOrthographicWidth, m_fOrthographicHeight, m_fNear, m_fFar, AEClipSpaceDepthRange::Native, AEClipSpaceYRange::OneToMinusOne );
			}

			m_bIsDirty = AEFALSE;
		}
	}

	if( yRange == AEClipSpaceYRange::MinusOneToOne )
		return m_matCachedMatrix[0];

	return m_matCachedMatrix[1];
}
	
void AEContextViewProperties::SetProjectionType( AEProjectionType eType )
{
	if( m_eProjectionType == eType )
		return;

	m_eProjectionType = eType;
	m_bIsDirty = AETRUE;
}
	
void AEContextViewProperties::SetCustomProjection( const AEMatrix4f &matProjection )
{
	m_eProjectionType = AEPROJECTIONTYPE_CUSTOM;
	m_matCachedMatrix[0] = matProjection;
	m_matCachedMatrix[1] = matProjection;
}
	
void AEContextViewProperties::SetFov( AEFLOAT32 fFovX, AEFLOAT32 fFovY )
{
	m_fFovX = fFovX;
	m_fFovY = fFovY;
	m_bIsDirty = AETRUE;
}

void AEContextViewProperties::GetFov( AEFLOAT32 &fFovX, AEFLOAT32 &fFovY ) const
{
	fFovX = m_fFovX;
	fFovY = m_fFovY;
}

void AEContextViewProperties::GetFinalFov( AEFLOAT32 &fFovX, AEFLOAT32 &fFovY ) const
{
}
	
AEFLOAT32 AEContextViewProperties::GetFovX( void ) const
{
	return m_fFovX;
}
	
AEFLOAT32 AEContextViewProperties::GetFovY( void ) const
{
	return m_fFovY;
}

void AEContextViewProperties::SetDisplayAspectRatio( AEFLOAT32 fDisplayAspectRatio )
{
	m_fDisplayAspectRatio = fDisplayAspectRatio;
	m_bIsDirty = AETRUE;
}

AEFLOAT32 AEContextViewProperties::GetDisplayAspectRatio( void ) const
{
	return m_fDisplayAspectRatio;
}

void AEContextViewProperties::SetNear( AEFLOAT32 fNear )
{
	m_fNear = fNear;
	m_bIsDirty = AETRUE;
}
	
void AEContextViewProperties::SetFar( AEFLOAT32 fFar )
{
	m_fFar = fFar;
	m_bIsDirty = AETRUE;
}
	
void AEContextViewProperties::SetClipPlanes( AEFLOAT32 fNear, AEFLOAT32 fFar )
{
	m_fFar = fFar;
	m_fNear = fNear;
	m_bIsDirty = AETRUE;
}

AEFLOAT32 AEContextViewProperties::GetNear( void ) const
{
	return m_fNear;
}
	
AEFLOAT32 AEContextViewProperties::GetFar( void ) const
{
	return m_fFar;
}
	
void AEContextViewProperties::GetClipPlanes( AEFLOAT32 &fNear, AEFLOAT32 &fFar) const
{
	fNear = m_fNear;
	fFar = m_fFar;
}

void AEContextViewProperties::SetLODScaling( AEFLOAT32 fScale )
{
	m_fLODScale = fScale;
	m_bIsDirty = AETRUE;
}

AEFLOAT32 AEContextViewProperties::GetLODScaling( void ) const
{
	return m_fLODScale;
}

void AEContextViewProperties::SetOrthographicSize( AEFLOAT32 fWidth, AEFLOAT32 fHeight )
{
	m_fOrthographicWidth = fWidth;
	m_fOrthographicHeight = fHeight;
	m_bIsDirty = AETRUE;
}
	
void AEContextViewProperties::SetOrthographicWidth( AEFLOAT32 fWidth )
{
	m_fOrthographicWidth = fWidth;
	m_bIsDirty = AETRUE;
}
	
void AEContextViewProperties::SetOrthographicHeight( AEFLOAT32 fHeight )
{
	m_fOrthographicHeight = fHeight;
	m_bIsDirty = AETRUE;
}

AEFLOAT32 AEContextViewProperties::GetOrthographicWidth( void )
{
	return m_fOrthographicWidth;
}

AEFLOAT32 AEContextViewProperties::GetOrthographicHeight( void )
{
	return m_fOrthographicHeight;
}
